/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.client.model.component;

import java.applet.Applet;
import java.applet.AppletContext;
import java.applet.AppletStub;
import java.applet.AudioClip;
import java.awt.Image;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.swing.JApplet;
import javax.swing.JComponent;


/*
 * AppletViewer - a simple Applet Viewer program.
 * @author  Ian Darwin, http://www.darwinsys.com/
 *
 * Heavily modified, though.
 */
public class AppletEnvironment implements AppletStub, AppletContext {

    /** The name of the Applet subclass */
    AppletInfo info;
     /** The Applet instance we are running, or null. Can not be a JApplet
     * until all the entire world is converted to JApplet. */
    JApplet applet = null;

    /** Construct the applet environment */
    public AppletEnvironment(AppletInfo info) throws Exception {
        super();
        this.info = info;
        init();
    }

    /**
     * Return the applet
     * 
     * @return applet
     */
    public JApplet getApplet() {
        return applet;
    }

    /**
     * Initialize the environment. This will construct an AppletAdapter as well
     * as instantiate the applet itself
     * 
     * @throws Exception
     */
    protected void init() throws Exception {
        applet = loadApplet(info.getName());  // sets ac and ai
        if (applet == null)
            return;

        // Now right away, tell the Applet how to find showStatus et al.
        applet.setStub(this);
    }

    /*
     * Load the Applet into memory. Should do caching.
     */
    JApplet loadApplet(String appletName) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        // get a Class object for the Applet subclass
        Class appletClass = Class.forName(appletName);
        // Construct an instance (as if using no-argument constructor)
        return (JApplet) appletClass.newInstance();
    }


    /****************** AppletStub ***********************/
    /** Called when the applet wants to be resized.  */
    @Override
    public void appletResize(int w, int h) {
        // applet.setSize(w, h);
    }

    /** Gets a reference to the applet's context.  */
    @Override
    public AppletContext getAppletContext() {
        return this;
    }

    /** Gets the base URL.  */
    public URL getCodeBase() {
        return getClass().getResource(".");
    }

    /** Gets the document URL.  */
    public URL getDocumentBase() {
        return getClass().getResource(".");
    }

    /** Returns the value of the named parameter in the HTML tag.  */
    public String getParameter(String name) {
        return info.getParameters().get(name);
    }

    /** Determines if the applet is active.  */
    public boolean isActive() {
        return true;
    }

    /************************ AppletContext ************************/
    /** Finds and returns the applet with the given name. */
    public Applet getApplet(String an) {
        return null;
    }

    /** Finds all the applets in the document */
    public Enumeration getApplets() {
        class AppletLister implements Enumeration {

            public boolean hasMoreElements() {
                return false;
            }

            public Object nextElement() {
                return null;
            }
        }
        return new AppletLister();
    }

    /** Create an audio clip for the given URL of a .au file */
    public AudioClip getAudioClip(URL u) {
        return null;
    }

    /** Look up and create an Image object that can be paint()ed */
    public Image getImage(URL u) {
        return null;
    }

    /** Request to overlay the current page with a new one - ignored */
    public void showDocument(URL u) {
    }

    /** as above but with a Frame target */
    public void showDocument(URL u, String frame) {
    }

    /** Called by the Applet to display a message in the bottom line */
    public void showStatus(String msg) {
    }

    /* StreamKey stuff - new in JDK1.4 */
    Map streamMap = new HashMap();

    /** Associate the stream with the key. */
    public void setStream(String key, InputStream stream) throws IOException {
        streamMap.put(key, stream);
    }

    public InputStream getStream(String key) {
        return (InputStream) streamMap.get(key);
    }

    public Iterator getStreamKeys() {
        return streamMap.keySet().iterator();
    }
}
